package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.CustomerUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CusUser;
import com.ruoyi.system.mapper.CustomerMapper;
import com.ruoyi.system.mapper.CustomerUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrdersMapper;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-12
 */
@Service
public class OrdersServiceImpl implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CustomerMapper customerMapper;


    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Orders selectOrdersById(String id) {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单列表
     *
     * @param orders 订单
     * @return 订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders) {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders) {
        if (StringUtils.isEmpty(orders.getEmail())) {
            CustomerUser user = customerMapper.selectUserByUserName(orders.getUserName());
            if (StringUtils.isNotNull(user)) {
                orders.setEmail(user.getEmail());
            }
        }
        Orders oldOrders = ordersMapper.selectOrdersByOrderNo(orders.getOrderNo());
        if (StringUtils.isNotNull(oldOrders)) {
            return ordersMapper.updateOrdersByOrderNo(orders);
        } else {
            return ordersMapper.insertOrders(orders);
        }
    }

    /**
     * 修改订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders) {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids) {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id) {
        return ordersMapper.deleteOrdersById(id);
    }

    @Override
    public Orders selectOrdersByOrderNo(String orderNo) {
        return ordersMapper.selectOrdersByOrderNo(orderNo);
    }
}
